<?php
class Lib extends CI_Controller {
	
	private $user_id;
	private $data;
	
	public function __construct()
	{
		parent::__construct();
		$this->user_utils->filter_login();
		$this->user_id = $this->user_utils->get_session_attribute(Variable::SESSION_USER_ID);
		
		$this->load->model('record_model');
		$this->load->library('utils/time_utils');

		//month list
		$month_list = $this->record_model->get_month_list($this->user_id);
		
		//tag list
		$this->load->model('tag_model');
		$tag_list = $this->tag_model->get_tag_list($this->user_id);
		
		//set data
		$this->data['type'] = 'lib';
		$this->data['view'] = 'lib/list';
		$this->data['month_list'] = $month_list;
		$this->data['tag_list'] = $tag_list;
		$this->data['tag_id'] = 0;
	}
	
	public function index()
	{
		$type = $this->session->userdata(Variable::SESSION_LIB_TYPE);
		if(!$type || $type == Variable::LIST_TODAY) $this->today();
		else if($type == Variable::LIST_YESTERDAY) $this->yesterday();
		else if($type == Variable::LIST_WEEK) $this->week();
		else if($type == Variable::LIST_MONTH) $this->month();
		else if($type == Variable::LIST_TAG) 
		{
			$tag_id = $this->session->userdata(Variable::SESSION_LIB_TAG_ID);
			$this->tag($tag_id ? $tag_id : 0);
		}
	}
	
	public function today($page = 1)
	{
		$this->data['page'] = $page;
		$this->data['category'] = Variable::LIST_TODAY;
		$this->data['category_name'] = Variable::LIST_TODAY_NAME;
		$this->session->set_userdata(Variable::SESSION_LIB_TYPE, Variable::LIST_TODAY);
	
		$range = $this->time_utils->get_today_range();
		//count
		$count = $this->record_model->get_list_count_by_time($this->user_id, $range[0], $range[1]);
		$this->data['count'] = $this->_get_page_count($count);
		
		//record list
		$from = ($page - 1) * Variable::LIST_SIZE;
		$list = $this->record_model->get_list_by_time($this->user_id, $range[0], $range[1], $from);
		$this->data['record_list'] = $list;
		
		//load view
		$this->load->view('member/index', $this->data);
	}
	
	public function yesterday($page = 1)
	{
		$this->data['page'] = $page;
		$this->data['category'] = Variable::LIST_YESTERDAY;
		$this->data['category_name'] = Variable::LIST_YESTERDAY_NAME;
		$this->session->set_userdata(Variable::SESSION_LIB_TYPE, Variable::LIST_YESTERDAY);
	
		$range = $this->time_utils->get_yesterday_range();
		//count
		$count = $this->record_model->get_list_count_by_time($this->user_id, $range[0], $range[1]);
		$this->data['count'] = $this->_get_page_count($count);
		
		//record list
		$from = ($page - 1) * Variable::LIST_SIZE;
		$list = $this->record_model->get_list_by_time($this->user_id, $range[0], $range[1], $from);
		$this->data['record_list'] = $list;
	
		//load view
		$this->load->view('member/index', $this->data);
	}
	
	public function week($page = 1)
	{
		$this->data['page'] = $page;
		$this->data['category'] = Variable::LIST_WEEK;
		$this->data['category_name'] = Variable::LIST_WEEK_NAME;
		$this->session->set_userdata(Variable::SESSION_LIB_TYPE, Variable::LIST_WEEK);
	
		$range = $this->time_utils->get_week_range();
		//count
		$count = $this->record_model->get_list_count_by_time($this->user_id, $range[0], $range[1]);
		$this->data['count'] = $this->_get_page_count($count);
		
		//record list
		$from = ($page - 1) * Variable::LIST_SIZE;
		$list = $this->record_model->get_list_by_time($this->user_id, $range[0], $range[1], $from);
		$this->data['record_list'] = $list;
	
		//load view
		$this->load->view('member/index', $this->data);
	}
	
	public function month($page = 1, $year = 0, $month = 0)
	{
		$this->data['page'] = $page;
		$this->data['category'] = Variable::LIST_MONTH;
		$this->data['category_name'] = Variable::LIST_MONTH_NAME;
		$this->session->set_userdata(Variable::SESSION_LIB_TYPE, Variable::LIST_MONTH);
		
		//record list
		if($year == 0 || $month == 0)
		{
			$year = date('Y');
			$month = date('m');
		}
	
		$range = $this->time_utils->get_month_range($year, $month);
		//count
		$count = $this->record_model->get_list_count_by_time($this->user_id, $range[0], $range[1]);
		$this->data['count'] = $this->_get_page_count($count);
		
		//record list
		$from = ($page - 1) * Variable::LIST_SIZE;
		$list = $this->record_model->get_list_by_time($this->user_id, $range[0], $range[1], $from);
		$this->data['record_list'] = $list;
	
		//load view
		$this->load->view('member/index', $this->data);
	}
	
	//the default value use for user type url
	public function tag($tag_id, $page = 1)
	{
		$this->data['page'] = $page;
		$this->data['tag_id'] = $tag_id;
		$this->data['category'] = Variable::LIST_TAG;
		
		$tag = $this->tag_model->get_tag_by_id($tag_id);
		if(!$tag) $tag_id = 0;
		
		$this->session->set_userdata(Variable::SESSION_LIB_TYPE, Variable::LIST_TAG);
		$this->session->set_userdata(Variable::SESSION_LIB_TAG_ID, $tag_id);
		
		//tag name
		$this->data['category_name'] = $tag_id == 0 ? '未分类' : $tag->tagName;

		$this->load->library('service/record_service');
		//count
		$count = $this->record_service->get_list_count_by_tag_id($this->user_id, $tag_id);
		$this->data['count'] = $this->_get_page_count($count);
		
		//record list
		$from = ($page - 1) * Variable::LIST_SIZE;
		$list = $this->record_service->get_list_by_tag_id($this->user_id, $tag_id, $from);
		$this->data['record_list'] = $list;
	
		//load view
		$this->load->view('member/index', $this->data);
	}
	
	public function _get_page_count($count)
	{
		$remain = $count % Variable::LIST_SIZE;
		$value = intval($count / Variable::LIST_SIZE);
		return $remain == 0 ? $value : $value + 1;
	}
}
?>